﻿var resUrl=document.getElementById('sername').value;
var res_Url=document.getElementById('resUrl').value;
var serhost=document.getElementById('serhost').value;
var version=document.getElementById('version').value;
var _customerId=$.util.getParam('customerId');
var _refereeId=$.util.getParam('refereeId');
var _myCity=document.getElementById('myCity').value;
var HEIGHT=$(window).height(),WIDTH=$(window).height();

var app=angular.module("app",['ui.router','ngAnimate','ngCookies']);


app.value('BasicData',{
	realname:"",
	phone:'',
	weixin:'',
	gender:'',
	height:'',
	weight:'',
	education:'',
	job:'',
	earning:'',
	province:'',
	city:'',
	house:'',
	car:'',
	onlyChild:'',
	religion:'',
	register:'',
	nation:'',
	shengxiao:'',
	constellation:'',
	marital:''
	
})
app.run(function($log, $rootScope, $http, $templateCache,$cacheFactory,$timeout,$cookieStore,$state){
		
		$rootScope.animateActive=false;
		
		$rootScope.pageManager={
			defaultPage:'hnindex',
			authPage:['pindex/0','account/0','loveme','loveeach'],
			halfAuthPage:['reg_s1','reg_s2','reg_s3','reg_s4','agent']
		}
		
		$rootScope.dialogUi={
			id:'',
			isShow:false,
			name:"",
			title:'',
			placeholder:'',
			callback:function(){}
		}
		
		$rootScope.uiObj={
					ishn:0,
					rebuildBase:0,
					culWidthtext:'',
					remApp:false,
					path:'',
					pathparams:'',
					frompath:'',
			    	pageloadingCtrl:true,
			    	layerloadingCtrl:false,
			    	loading:true,
			    	isactive:'',
			    	pageAnimationClass:'',
			    	pageAnimationForce:false,
			    	isfixed:false,
			    	offtop:'',
			    	ischeck:0,
			    	swindex:1
		 }
		$rootScope.router={
			btnlist:[
				{class:'tagFind',img:resUrl+'wx_assets/hn/images/p_icon1',cur:'discover'},
				{class:'tagUser',img:resUrl+'wx_assets/hn/images/p_icon2',cur:'pindex'}
			]
		}
		$rootScope.checkIsInput=function(){
			var data=$cookieStore.get('BasicData')
			if(data){
				if(!data.realname)
				{
					return false;
				}
				else if(!data.house)
				{
					return false;
				}
				else if(!data.card_front)
				{
					return false;
				}
				
				return true
			}
			else
			{
				return false;
			}
		}
		$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
				$rootScope.uiObj.path=toState.name;
				$rootScope.uiObj.frompath=fromState.name;
				$rootScope.uiObj.pathparams=toParams.hn;
				
				if(fromState.name=='pindex'&&toState.name=='reg_s1')
				{
					$rootScope.uiObj.rebuildBase=1;
					
				}
				
				$rootScope.checklogin($rootScope.uiObj.path,$rootScope.uiObj.frompath,$rootScope.uiObj.pathparams,event);
				
//				checklogin($rootScope.uiObj.path);
		    	$rootScope.uiObj.loading = false;
        		$rootScope.uiObj.layerloadingCtrl=true;
            	$rootScope.uiObj.isactive = '';
            	
            	console.log($cookieStore.get('BasicData'))
//          	
				for(var i=0;i<$rootScope.router.btnlist.length;i++)
				{
					if($rootScope.router.btnlist[i].cur.indexOf(toState.name)>=0)
					{
						$rootScope.router.btnlist[i].active=true;
					}
					else
					{
						$rootScope.router.btnlist[i].active=false;
					}
				}

//          	if(fromState.name==''&&$rootScope.uiObj.path!='hnindex'&&!$rootScope.checkIsInput())
//          	{
//          		location.href='#/hnindex'
//          	}
		});
		$rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){
			window.scrollTo(0,0)
		})
		  //全局方法和变量
		  
		  $rootScope.checklogin=function(path,frompath,ishn,ev){
		  
//		  	console.log($rootScope.uiObj.ischeck)
		  	if($rootScope.uiObj.ischeck==0)
		  	{
			  	if(isbase==1&&iscondition==1)
			  	{
			  		if(path.indexOf('reg_')>=0||path.indexOf('agent')>=0||path.indexOf('hnindex')>=0)
			  		{
			  			location.href='#/pindex/0';
//			  			$state.go('pindex',{hn:0});
			  		}
			  		
			  	}
			  	else if(isbase==0)
			  	{
			  		
			  		if(path!='hnindex'&&path!='loveIndex')
			  		{
			  			if(path=='pindex'||path=='account'||path=='discover'||path=='mycode')
			  			{
			
			  				if(ishn==0)
			  				{
			  					location.href='#/hnindex';
//			  					$state.go('hnindex');
			  				}
			  			}
			  			else
			  			{
			  				location.href='#/hnindex';
//			  				$state.go('hnindex');
			  			}
			  			
			  		} 
			  		
			  	}
//			  	else if(iscondition==0)
//			  	{
//			  		if(path!='reg_s4'&&path!='loveIndex')
//			  		{
//			  			event.preventDefault();
//						$state.go('reg_s4');
//			  		}
//			  		
//			  	}
//			  	else if(isagent==0)
//			  	{
//			  		console.log('跳');
//			  		if(path!='agent')
//			  		{
//			  			
//			  			$rootScope.uiObj.ischeck=1;
//			  			$state.go('agent');
//			  		}
//			  		
//			  	}
				$rootScope.uiObj.ischeck=1;
				
			}
		  }
		  
		  $rootScope.callSuccessHttp = function() {
	
			$rootScope.uiObj.loading = false;
			
			$.util.pageLoading.hide();
			
			$rootScope.uiObj.isactive = 'active';
			$rootScope.uiObj.layerloadingCtrl=false;
			layer.closeAll();
			
		}
		  window.onscroll = function(){
		  		if(!$rootScope.uiObj.isfixed)
		  		{
		  			
		  			$rootScope.uiObj.offtop=getScrollTop();
		  		}	
		  }
		  
		  $rootScope._preStep=function(){
		  	 switch($rootScope.uiObj.path)
				{
					case 'reg_s1':
					location.href='#/hnindex';
					break;
					case 'reg_s2':
					location.href='#/reg_s1';
					break;
					case 'reg_s3':
					location.href='#/reg_s2';
					break;
					case 'reg_s4':
					location.href='#/reg_s3';
					break;
				}
		  }
		  
		  $rootScope._nextStep=function(bool,arr,callback,iscookies){
		  	if(bool)
		  	{
		  		var obj=new Object();
				for(var i=0;i<arr.length;i++)
				{
					if(arr[i].value=='f'||arr[i].value=='市县'||arr[i].value=='省份')
					{
						arr[i].value='';
					}
					
					if(arr[i].child)
					{
						for(var y=0;y<arr[i].child.length;y++)
						{
							obj[arr[i].child[y].id]=arr[i].child[y].value;
						}
						
					}
					else if(arr[i].isrange)
					{
						var str=UpperFirstLetter(arr[i].id);
						obj['cmin'+str]=(arr[i].data.min!='f'?arr[i].data.min:'');
						obj['cmax'+str]=(arr[i].data.max!='f'?arr[i].data.max:'');
					}
					else
					{
						obj[arr[i].id]=arr[i].value;
						
					}
					
				}
				
				if($cookieStore.get('BasicData')&&!iscookies)
				{
					var data=angular.extend($cookieStore.get('BasicData'),obj);
				}
				else
				{
					var data=obj;
				}
				if(!iscookies)
				{
					$cookieStore.put('BasicData',data);
				}
				console.log($cookieStore.get('BasicData'))
				switch($rootScope.uiObj.path)
				{
					
					case 'reg_s1':
					if(!$rootScope.uiObj.rebuildBase)
					{
						callback(data);
					}
					else
					{
						location.href='#/reg_s2';
					}
					break;
					case 'reg_s2':
					location.href='#/reg_s3';
					break;
					case 'reg_s3':
					if(_refereeId)
					{
						data.refereeId=_refereeId;
					}
					callback(data);
					break;
					case 'reg_s4':
					var tmp=Object.keys(data);
					var tmpData=new Object();
					for(var i=0;i<tmp.length;i++)
					{
						var str=data[tmp[i]];
						tmp[i]=tmp[i].replace('_','');
						tmpData[tmp[i]]=str;	
					}
					callback(tmpData);
					break;
					case 'mmcondition':
					callback(data);
					break;
				}
				
		  	}
		  }
		  
		  $rootScope.closeDialog=function(){
		  	angular.element(document.getElementById('dialogUi_mask')).remove();
		  	$rootScope.dialogUi.isShow=false;
		  	
		  }
		  
		  $rootScope.preventBody=function(){
		  	$rootScope.uiObj.isfixed=true;
		  }
		  
		  $rootScope.repreventBody=function(){
		  	$rootScope.uiObj.isfixed=false;
		  }
		  
		  $rootScope.$watch('dialogUi.isShow',function(n,o,s){
		  	if(n!=o&&n)
		  	{
		  		$rootScope.preventBody();
		  		
		  	}
		  	else if(n!=o&&!n)
		  	{
		  		$rootScope.repreventBody();

		  	}
		  })
		  
          $rootScope.initSwiper = function (_class,swcallBackfuns) {
				$rootScope.mySwiper = new Swiper(_class, {
					slidesPerView :'auto',
					pagination: '.swiper-pagination',
        			paginationType: 'fraction',
					onTransitionEnd:swcallBackfuns._onTransitionEnd
				});
				
//        	 	console.log($rootScope.mySwiper);

          }
          $rootScope.updateSwiper=function(){
          			$rootScope.mySwiper.update();
          }
          $rootScope.loadLoyer=function(){
          	layer.open({
            		content:'<div class="spinner">'
            				+'<div class="spinner-container container1">'
            					+'<div class="circle1"></div>'
            					+'<div class="circle2"></div>'
            					+'<div class="circle3"></div>'
            					+'<div class="circle4"></div>'
            				+'</div>'
            				+'<div class="spinner-container container2">'
            					+'<div class="circle1"></div>'
            					+'<div class="circle2"></div>'
            					+'<div class="circle3"></div>'
            					+'<div class="circle4"></div>'
            				+'</div>'
            				+'<div class="spinner-container container3">'
            					+'<div class="circle1"></div>'
            					+'<div class="circle2"></div>'
            					+'<div class="circle3"></div>'
            					+'<div class="circle4"></div>'
            				+'</div>',
            		style:'text-align:center;',
            		shadeClose: false
            })
          }
  });
app.config(function($stateProvider,$urlRouterProvider,$httpProvider){
	$urlRouterProvider.otherwise("/hnindex");
    $stateProvider.state("hnindex",{
    	url:'/hnindex',
        templateUrl: resUrl+'wx_api/Module/hn/views/agreement.html'+version,
        controller:'agreementCtrl'
    }).state("reg_s1",{
    	url:'/reg_s1',
        templateUrl: resUrl+'wx_api/Module/hn/views/reg_s1.html'+version,
        controller:'regs1Ctrl'
    }).state("reg_s2",{
    	url:'/reg_s2',
        templateUrl: resUrl+'wx_api/Module/hn/views/reg_s2.html'+version,
        controller:'regs2Ctrl'
    }).state("reg_s3",{
    	url:'/reg_s3',
        templateUrl: resUrl+'wx_api/Module/hn/views/reg_s3.html'+version,
        controller:'regs3Ctrl'
    }).state("reg_s4",{
    	url:'/reg_s4',
        templateUrl: resUrl+'wx_api/Module/hn/views/reg_s4.html'+version,
        controller:'regs4Ctrl'
    }).state("agent",{
    	url:'/agent',
        templateUrl: resUrl+'wx_api/Module/hn/views/agent.html'+version,
        controller:'agentCtrl'
    }).state("pindex",{
    	url:'/pindex/:hn',
        templateUrl: resUrl+'wx_api/Module/hn/views/pindex.html'+version,
        controller:'pindexCtrl'
    }).state("mycode",{
    	url:'/mycode',
        templateUrl: resUrl+'wx_api/Module/hn/views/mycode.html'+version,
        controller:'mycodeCtrl'
    }).state("loveme",{
    	url:'/loveme',
        templateUrl: resUrl+'wx_api/Module/hn/views/loveme.html'+version,
        controller:'lovemeCtrl'
    }).state("loveeach",{
    	url:'/loveeach',
        templateUrl: resUrl+'wx_api/Module/hn/views/loveeach.html'+version,
        controller:'loveeachCtrl'
    }).state("fmatch",{
    	url:'/fmatch',
        templateUrl: resUrl+'wx_api/Module/hn/views/fmatch.html'+version,
        controller:'fmatchCtrl'
    }).state("unreg",{
    	url:'/unreg',
        templateUrl: resUrl+'wx_api/Module/hn/views/unreg.html'+version,
        controller:'unregCtrl'
    }).state("discover",{
    	url:'/discover/:hn',
        templateUrl: resUrl+'wx_api/Module/hn/views/discover.html'+version,
        controller:'discoverCtrl'
    }).state("loveIndex",{
    	url:'/loveIndex/:uid',
        templateUrl: resUrl+'wx_api/Module/hn/views/loveIndex.html'+version,
        controller:'loveIndexCtrl'
    }).state("account",{
    	url:'/account/:hn',
        templateUrl: resUrl+'wx_api/Module/hn/views/account.html'+version,
        controller:'accountCtrl'
    }).state("withdraw",{
    	url:'/withdraw',
        templateUrl: resUrl+'wx_api/Module/hn/views/withdraw.html'+version,
        controller:'withdrawCtrl'
    }).state("recharge",{
    	url:'/recharge',
        templateUrl: resUrl+'wx_api/Module/hn/views/recharge.html'+version,
        controller:'rechargeCtrl'
    }).state("mmcondition",{
    	url:'/mmcondition',
        templateUrl: resUrl+'wx_api/Module/hn/views/mmcondition.html'+version,
        controller:'mmconditionCtrl'
    })
    
    $httpProvider.interceptors.push('timestampMarker');
});

//loading
app.factory('timestampMarker', ["$rootScope", function ($rootScope) {
    var timestampMarker = {
        request: function (config) {
        	if($rootScope.uiObj.layerloadingCtrl)
            {
    			
            	$.util.pageLoading.show();
//          	$rootScope.loadLoyer();
            }
            config.requestTimestamp = new Date().getTime();
            return config;
        },
        response: function (response) {
            response.config.responseTimestamp = new Date().getTime();
            return response;
            
        }
    };
    return timestampMarker;
}]);

//自定义过滤器--时间

app.filter('chatDate',function(){
	return function(input){
		//input就是我们传入的字符串
		if(input)
		{
			input=input.replace(/-/, "/").replace(/-/, "/");
			var date1=new Date(input);  //开始时间
			var date2=new Date();    //结束时间
			var date3=date2.getTime()-date1.getTime()  //时间差的毫秒数
			 
			
			//计算出相差天数
			var days=Math.floor(date3/(24*3600*1000))
			 
			//计算出小时数
			
			var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数
			var hours=Math.floor(leave1/(3600*1000))
			//计算相差分钟数
			var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
			var minutes=Math.floor(leave2/(60*1000)) 
//			alert(" 相差 "+days+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒")
			var myDate = new Date(input);
			var result='';
			if(days==0)
			{
				if(hours==0)
				{
					if(minutes==0)
					{
						minutes=1;
					}
					result=minutes+'分钟前';
				}
				else
				{
					result=hours+'小时前';
				}
			}
			else
			{
				result=days+'天前';
			}
			
			return result.toString();
			
		}
	}
})
//限制点赞显示人数
app.filter('praiseFilter',function(){
	return function(input,limit){
		if(input.length>limit)
		{
			input.splice(limit,input.length-limit);
		}
		return input;
	}
})
//解析html
app.filter(  
    'to_trusted', ['$sce', function ($sce) { 
        return function (text) {  
        	
            return $sce.trustAsHtml(text);  
        }  
    }]  
)
app.filter('textoverflow',function(){
	return function(input,limit){
		if(!input)
		{
			input="匿名";
		}
		if(input.length>limit)
		{
			input=input.substring(0,limit)+'...';
		}
		return input;
	}
})
app.filter('textoverflow_byfans',function(){
	return function(input,limit){
		if(!input)
		{
			input="未关注好友";
		}
		if(input.length>limit)
		{
			input=input.substring(0,limit)+'...';
		}
		return input;
	}
})
app.filter('int',function(){
	return function(input){
		input=parseInt(input);
		return input;
	}
})

app.filter("jsonDate", function($filter) {
      return function(input, format) {
           //转成指定格式
           if(input)
           {
           		input=input.replace(/-/, "/").replace(/-/, "/");
           }
           return $filter("date")(new Date(input),'yyyy-MM-dd');
      };
});